package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.PropertyLoader;
public class CabinetPage {

    protected String pageUrl  = PropertyLoader.loadProperty("site.cabinet.url");
    protected static  WebDriver driver;

    /*---------------locators---------------------------------------------------------------*/
    private static By NameField = By.id("reciever_name");
    private static By LocalityField = By.id("suggest_locality");
    private static By PhoneField = By.id("reciever_phone");
    private static By EmailField = By.id("reciever_email");
    private static By Logo = By.id("logo");


    /*---------------constructor---------------------------------------------------------------*/
    public CabinetPage (WebDriver driver)
    {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    /*---------------methods---------------------------------------------------------------*/
    public void open()
    {
        driver.get(pageUrl);

    }

    public String getPageUrl() {
        return pageUrl;
    }

    public void fillFormForNewClient(String name, String locality, String phone, String email) {

        driver.findElement(NameField).clear();
        driver.findElement(NameField).sendKeys(name);
        driver.findElement(LocalityField).clear();
        //driver.findElement(LocalityField).sendKeys(locality);
        driver.findElement(LocalityField).sendKeys(Keys.ARROW_DOWN);
        (new WebDriverWait(driver, 20))
                .until(ExpectedConditions.presenceOfElementLocated(By.className("suggestion-i")));

        driver.findElement(By.className("suggestion-i")).click();
        driver.findElement(NameField).click();
        driver.findElement(PhoneField).clear();
        driver.findElement(PhoneField).sendKeys(phone);
        driver.findElement(EmailField).clear();
        driver.findElement(EmailField).sendKeys(email);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public Boolean isSubmitContactInfoAvilable() {

        return driver.findElement(By.xpath("//*[@id=\"step_contacts\"]/div/div[1]/div[3]/div/span")).getAttribute("class").equals("btn-link check-step-btn-link");

    }

    // get url without GET parameters
    public String getPath(String url) {

        String[] pathParts = url.split("\\?");
        String path = pathParts[0];
        return path;

    }






}
